//
//  JKToolExtension.swift
//  JKTools
//
//  Created by 姜奎 on 2024/4/25.
//

import Foundation

extension ShellOutCommand {
    static func zip(_ sourcePath: String, toPathURL: String, password: String? = nil) -> ShellOutCommand {

        var shell = "\(Bundle.bin(forResource: "jktool")!.path) zip --source \(sourcePath) --path \(toPathURL)"
        if let password = password, password.isEmpty == false {
            shell.connected(spaceCommand: "--password \(password)")
        }
        return ShellOutCommand(string: shell)
    }
    
    static func unzip(_ sourcePath: String, toPathURL: String, password: String? = nil) -> ShellOutCommand {
        var shell = "\(Bundle.bin(forResource: "jktool")!.path) unzip --source \(sourcePath) --path \(toPathURL)"
        if let password = password, password.isEmpty == false {
            shell.connected(spaceCommand: "--password \(password)")
        }
        return ShellOutCommand(string: shell)
    }

    static func updateJKTool() -> ShellOutCommand {
        let shell = "\(Bundle.bin(forResource: "jktool")!.path) version update"
        return ShellOutCommand(string: shell)
    }

    static func resetJKTool() -> ShellOutCommand {
        let shell = "\(Bundle.bin(forResource: "jktool")!.path) version reset"
        return ShellOutCommand(string: shell)
    }

    static func createProject(_ name: String) -> ShellOutCommand {
        let shell = "\(Bundle.bin(forResource: "jktool")!.path) version reset"
        return ShellOutCommand(string: shell)
    }
}

extension Foundation.Bundle {
    static func bin(forResource name: String, withExtension ext: String? = nil) -> URL? {
        return Bundle.main.url(forResource: name, withExtension: ext, subdirectory: "bin")
    }

    static func zshShare(forResource name: String, withExtension ext: String? = nil) -> URL? {
        return Bundle.main.url(forResource: name, withExtension: ext, subdirectory: "share/zsh/site-functions")
    }

    static func bashShare(forResource name: String, withExtension ext: String? = nil) -> URL? {
        return Bundle.main.url(forResource: name, withExtension: ext, subdirectory: "share/bash_profile/site-functions")
    }

    static func appleScript(forResource name: String, withExtension ext: String? = nil) -> URL? {
        return Bundle.main.url(forResource: name, withExtension: ext, subdirectory: "AppleScript")
    }

    static func xcodeproj(forResource name: String, withExtension ext: String? = nil) -> URL? {
        return Bundle.main.url(forResource: name, withExtension: ext, subdirectory: "private/gems/xcodeproj-1.27.0/bin")
    }
}
